home *** CD-ROM | disk | FTP | other *** search
/ Gamers Delight 2 / Gamers Delight 2.iso / Aminet / game / role / ZIP_1_0.lha / src / cursesio.c < prev    next >
C/C++ Source or Header  |  1992-10-13  |  9KB  |  538 lines

  1. /*
  2.  * cursesio.c
  3.  *
  4.  * Curses based screen I/O. This is an operating system specific screen I/O
  5.  * module that should work for VMS and UNIX systems. Some UNIX systems may 
  6.  * need cursesX.h instead of curses.h.
  7.  *
  8.  * The Z machine screen handling is based on a VT100, hey don't blame me, blame
  9.  * Infocom ;-). If you know how a VT100 behaves and think you can emulate it
  10.  * then you stand a good chance of porting this to some other type of display.
  11.  * If not read this source for inspiration.
  12.  *
  13.  * Mark Howell 28-Jul-1992 V1.0
  14.  *
  15.  */
  16.  
  17. #include "ztypes.h"
  18. #include <curses.h>
  19.  
  20. /* Local variables */
  21.  
  22. static int windows_started = FALSE;
  23. static int cursor_saved = OFF;
  24. static int saved_row = 0;
  25. static int saved_col = 0;
  26.  
  27. /*
  28.  * initialise_screen
  29.  *
  30.  * Do any screen initialisation including setting the global variables
  31.  * screen_rows and screen_cols.
  32.  *
  33.  */
  34.  
  35. #ifdef __STDC__
  36. void initialize_screen (void)
  37. #else
  38. void initialize_screen ()
  39. #endif
  40. {
  41.  
  42.     /* Start curses */
  43.  
  44.     initscr ();
  45.     windows_started = TRUE;
  46.  
  47.     /* Set screen_rows and screen_cols from curses */
  48.  
  49.     screen_rows = LINES;
  50.     screen_cols = COLS;
  51.  
  52.     /* Clear screen and display banner */
  53.  
  54.     clear_screen ();
  55.     move_cursor (screen_rows / 2, (screen_cols - sizeof ("The story is loading...")) / 2);
  56.     addstr ("The story is loading...");
  57.     refresh ();
  58.  
  59. }/* initialize_screen */
  60.  
  61. /*
  62.  * restart_screen
  63.  *
  64.  * Reset the screen back the to its state after initialise_screen.
  65.  *
  66.  */
  67.  
  68. #ifdef __STDC__
  69. void restart_screen (void)
  70. #else
  71. void restart_screen ()
  72. #endif
  73. {
  74.  
  75.     /* Reset attributes, clear display and put cursor in bottom left corner */
  76.  
  77.     set_attribute (NORMAL);
  78.     clear_screen ();
  79.     move_cursor (screen_rows, 1);
  80.  
  81. }/* restart_screen */
  82.  
  83. /*
  84.  * reset_screen
  85.  *
  86.  * Reset the screen before exiting program.
  87.  *
  88.  */
  89.  
  90. #ifdef __STDC__
  91. void reset_screen (void)
  92. #else
  93. void reset_screen ()
  94. #endif
  95. {
  96.  
  97.     /* Turn off curses */
  98.  
  99.     if (windows_started == TRUE) {
  100. #ifdef VMS
  101.         output_string ("[Hit any key to continue.]");
  102.         (void) input_character ();
  103. #endif /* VMS */
  104.         endwin ();
  105.     }
  106.     windows_started = FALSE;
  107.  
  108. }/* reset_screen */
  109.  
  110. /*
  111.  * clear_screen
  112.  *
  113.  * Clear the whole screen.
  114.  *
  115.  */
  116.  
  117. #ifdef __STDC__
  118. void clear_screen (void)
  119. #else
  120. void clear_screen ()
  121. #endif
  122. {
  123.  
  124.     clear ();
  125.  
  126. }/* clear_screen */
  127.  
  128. /*
  129.  * select_status_window
  130.  *
  131.  * Put the cursor in the status window. The cursor position from the test window
  132.  * should be saved and the cursor moved to the home position.
  133.  *
  134.  */
  135.  
  136. #ifdef __STDC__
  137. void select_status_window (void)
  138. #else
  139. void select_status_window ()
  140. #endif
  141. {
  142.  
  143.     save_cursor_position ();
  144.     refresh ();
  145.     move_cursor (1, 1);
  146.  
  147. }/* select_status_window */
  148.  
  149. /*
  150.  * select_text_window
  151.  *
  152.  * Restore the cursor in the text window after it has been moved to the status
  153.  * window.
  154.  *
  155.  */
  156.  
  157. #ifdef __STDC__
  158. void select_text_window (void)
  159. #else
  160. void select_text_window ()
  161. #endif
  162. {
  163.  
  164.     restore_cursor_position ();
  165.     refresh ();
  166.  
  167. }/* select_text_window */
  168.  
  169. /*
  170.  * create_status_window
  171.  *
  172.  * Create a window of size status_size x screen_cols at the top of the screen.
  173.  * This routine is also called if the status window needs to be resized.
  174.  * Basically this is a set scrolling region request. The text window is the
  175.  * scrolling region, and the status reagion is fixed.
  176.  *
  177.  */
  178.  
  179. #ifdef __STDC__
  180. void create_status_window (void)
  181. #else
  182. void create_status_window ()
  183. #endif
  184. {
  185.  
  186. }/* create_status_window */
  187.  
  188. /*
  189.  * delete_status_window
  190.  *
  191.  * Delete any previously created status window. Do not erase contents.
  192.  *
  193.  */
  194.  
  195. #ifdef __STDC__
  196. void delete_status_window (void)
  197. #else
  198. void delete_status_window ()
  199. #endif
  200. {
  201.  
  202. }/* delete_status_window */
  203.  
  204. /*
  205.  * clear_line
  206.  *
  207.  * Clear the line from the current cursor position to the end of the line.
  208.  *
  209.  */
  210.  
  211. #ifdef __STDC__
  212. void clear_line (void)
  213. #else
  214. void clear_line ()
  215. #endif
  216. {
  217.  
  218.     clrtoeol ();
  219.  
  220. }/* clear_line */
  221.  
  222. /*
  223.  * clear_text_window
  224.  *
  225.  * Clear the text window, leaving the cursor in the bottom left corner.
  226.  *
  227.  */
  228.  
  229. #ifdef __STDC__
  230. void clear_text_window (void)
  231. #else
  232. void clear_text_window ()
  233. #endif
  234. {
  235.     int i;
  236.  
  237.     for (i = status_size + 1; i <= screen_rows; i++) {
  238.         move_cursor (i, 1);
  239.         clear_line ();
  240.     }
  241.  
  242. }/* clear_text_window */
  243.  
  244. /*
  245.  * clear_status_window
  246.  *
  247.  * Clear the status window, leaving the cursor in the top left corner.
  248.  *
  249.  */
  250.  
  251. #ifdef __STDC__
  252. void clear_status_window (void)
  253. #else
  254. void clear_status_window ()
  255. #endif
  256. {
  257.     int i;
  258.  
  259.     for (i = status_size; i; i--) {
  260.         move_cursor (i, 1);
  261.         clear_line ();
  262.     }
  263.  
  264. }/* clear_status_window */
  265.  
  266. /*
  267.  * move_cursor
  268.  *
  269.  * Move the cursor position. Top left corner = (1, 1).
  270.  *
  271.  */
  272.  
  273. #ifdef __STDC__
  274. void move_cursor (int row, int col)
  275. #else
  276. void move_cursor (row, col)
  277. int row;
  278. int col;
  279. #endif
  280. {
  281.  
  282.     move (row - 1, col - 1);
  283.  
  284. }/* move_cursor */
  285.  
  286. /*
  287.  * save_cursor_position
  288.  *
  289.  * Save the position of the cursor. Cannot be called recursively.
  290.  *
  291.  */
  292.  
  293. #ifdef __STDC__
  294. void save_cursor_position (void)
  295. #else
  296. void save_cursor_position ()
  297. #endif
  298. {
  299.  
  300.     if (cursor_saved == OFF) {
  301.         getyx (stdscr, saved_row, saved_col);
  302.         cursor_saved = ON;
  303.     }
  304.  
  305. }/* save_cursor_position */
  306.  
  307. /*
  308.  * restore_cursor_position
  309.  *
  310.  * Restore the position of the cursor. Cannot be called recursively.
  311.  *
  312.  */
  313.  
  314. #ifdef __STDC__
  315. void restore_cursor_position (void)
  316. #else
  317. void restore_cursor_position ()
  318. #endif
  319. {
  320.  
  321.     if (cursor_saved == ON) {
  322.         move (saved_row, saved_col);
  323.         cursor_saved = OFF;
  324.     }
  325.  
  326. }/* restore_cursor_position */
  327.  
  328. /*
  329.  * set_attribute
  330.  *
  331.  * Set a video attribute. The attributes required are normal, reverse, bold,
  332.  * blink and underscore. The minimum set of attributes is normal and reverse.
  333.  *
  334.  */
  335.  
  336. #ifdef __STDC__
  337. void set_attribute (int attribute)
  338. #else
  339. void set_attribute (attribute)
  340. int attribute;
  341. #endif
  342. {
  343.  
  344.     switch (attribute) {
  345. #ifdef VMS
  346.         case REVERSE:    setattr (_REVERSE); break;
  347.         case BOLD:       setattr (_BOLD); break;
  348.         case BLINK:      setattr (_BLINK); break;
  349.         case UNDERSCORE: setattr (_UNDERLINE); break;
  350.  
  351.         default:         clrattr (_REVERSE | _BOLD | _BLINK | _UNDERLINE);
  352. #else /* !VMS */
  353.         case REVERSE:
  354.         case BOLD:
  355.         case BLINK:
  356.         case UNDERSCORE: standout (); break;
  357.  
  358.         default:         standend ();
  359. #endif /* VMS */
  360.     }
  361.  
  362. }/* set_attribute */
  363.  
  364. /*
  365.  * display_char
  366.  *
  367.  * Write a character at the current cursor position.
  368.  *
  369.  */
  370.  
  371. #ifdef __STDC__
  372. void display_char (int c)
  373. #else
  374. void display_char (c)
  375. int c;
  376. #endif
  377. {
  378.  
  379.     addch (c);
  380.  
  381. }/* display_char */
  382.  
  383. /*
  384.  * fatal
  385.  *
  386.  * Display a fatal error message and exit.
  387.  *
  388.  */
  389.  
  390. #ifdef __STDC__
  391. void fatal (const char *s)
  392. #else
  393. void fatal (s)
  394. const char *s;
  395. #endif
  396. {
  397.  
  398.     reset_screen ();
  399.     printf ("\nFatal error: %s (PC = %lx)\n", s, pc);
  400.     exit (EXIT_SUCCESS);
  401.  
  402. }/* fatal */
  403.  
  404. /*
  405.  * input_character
  406.  *
  407.  * Read one character without echo.
  408.  *
  409.  */
  410.  
  411. #ifdef __STDC__
  412. char input_character (void)
  413. #else
  414. char input_character ()
  415. #endif
  416. {
  417.     int c;
  418.  
  419.     refresh ();
  420.     noecho ();
  421.     crmode ();
  422.     c = getch ();
  423.     nocrmode ();
  424.     echo ();
  425.  
  426.     return (c);
  427.  
  428. }/* input_character */
  429.  
  430. /*
  431.  * input_line
  432.  *
  433.  * Read a line into the global buffer input. This buffer is allocated
  434.  * screen_cols space. input[0] contains the maximum number of characters to
  435.  * read. input[1] should be set to the actual number of characters read. The
  436.  * characters start at position input[2]. Do not overrun the buffer!
  437.  *
  438.  */
  439.  
  440. #ifdef __STDC__
  441. void input_line (void)
  442. #else
  443. void input_line ()
  444. #endif
  445. {
  446.  
  447.     refresh ();
  448.     input[1] = 0;
  449.     input[2] = '\0';
  450.     getstr (&input[2]);
  451.     input[1] = strlen (&input[2]);
  452.     if (input[1] > input[0])
  453.         input[1] = input[0];
  454.     scroll_line ();
  455.  
  456. }/* input_line */
  457.  
  458. /*
  459.  * scroll_line
  460.  *
  461.  * This routine does the equivalent of putc('\n') in the text window.
  462.  *
  463.  */
  464.  
  465. #ifdef __STDC__
  466. void scroll_line (void)
  467. #else
  468. void scroll_line ()
  469. #endif
  470. {
  471.  
  472.     move_cursor (status_size + 1, 1);
  473.     deleteln ();
  474.     move_cursor (screen_rows, 1);
  475.     refresh ();
  476.  
  477. }/* scroll_line */
  478.  
  479. /*
  480.  * fit_line
  481.  *
  482.  * This routine determines whether a line of text will still fit
  483.  * on the screen.
  484.  *
  485.  * line : Line of text to test.
  486.  * pos  : Length of text line (in characters).
  487.  * max  : Maximum number of characters to fit on the screen.
  488.  *
  489.  */
  490.  
  491. #ifdef __STDC__
  492. int fit_line (const char *line, int pos, int max)
  493. #else
  494. int fit_line (line, pos, max)
  495. const char *line;
  496. int pos;
  497. int max;
  498. #endif
  499. {
  500.  
  501.     return (pos < max);
  502.  
  503. }/* fit_line */
  504.  
  505. /*
  506.  * print_status
  507.  *
  508.  * Print the status line (type 3 games only).
  509.  *
  510.  * argv[0] : Location name
  511.  * argv[1] : Moves/Time
  512.  * argv[2] : Score
  513.  *
  514.  * Depending on how many arguments are passed to this routine
  515.  * it is to print the status line. The rendering attributes
  516.  * and the status line window will be have been activated
  517.  * when this routine is called. It is to return FALSE if it
  518.  * cannot render the status line in which case the interpreter
  519.  * will use display_char() to render it on its own.
  520.  *
  521.  * This routine has been provided in order to support
  522.  * proportional-spaced fonts.
  523.  *
  524.  */
  525.  
  526. #ifdef __STDC__
  527. int print_status (int argc, char *argv[])
  528. #else
  529. int print_status (argc, argv)
  530. int argc;
  531. char *argv[];
  532. #endif
  533. {
  534.  
  535.     return (FALSE);
  536.  
  537. }/* print_status */
  538.